package com.gxa.user.recycling.controller;

import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import com.gxa.core.recycling.util.DateUtil;
import com.gxa.core.recycling.util.TimeUtil;
import com.gxa.user.recycling.repository.ImagesRepository;
import com.gxa.user.recycling.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xhy
 * @Date 2021/12/2 16:34
 */
//@Transactional(rollbackFor = Throwable.class)
@RestController
@RequestMapping("/user/my")
@Transactional(rollbackFor = {Throwable.class})
public class MineController {

    @Autowired
    BannerService bannerService;

    @PostMapping("/findPictures")
    @ApiOperation("轮播图片")
    public Map findPictures(){
        List<Banner> allList = bannerService.findAllList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("轮播图片发送成功");
        jsonResult.setData(allList);
        return jsonResult.getDataMap();
    }

    @PostMapping("/insertUser")
    @ApiOperation("关注公众号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wechat",value = "用户微信号", required = true)
    })
    public Map insertUser(String wechat
                          ){
        User user = new User();
        user.setPhone("");
        user.setWechat(wechat);
        user.setAllPoints(0);
        user.setDays(0);
        user.setLastLogin(new Timestamp(System.currentTimeMillis()));
        user.setLevelId("VIP1");
        user.setNickName("贵客10086");
        user.setNowPoints(0);
        user.setType("关注用户");
        userService.insert(user);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("关注成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private MedalCollectionService medalCollectionService;

    @PostMapping("/findMine")
    @ApiOperation("我的")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map findMine(Long id){
        Score score = new Score();
        score.setSerson("签到");
        List<Score> select = scoreService.select(score);
        User user = userService.selectById(id);
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setUid(id);
        List<Recycleorder> select1 = recycleorderService.select(recycleorder);

        MedalCollection medalCollection = new MedalCollection();
        medalCollection.setUid(id);
        List<MedalCollection> select2 = medalCollectionService.select(medalCollection);

        recycleorder.setState("待接单");
        List<Recycleorder> select3 = recycleorderService.select(recycleorder);
        recycleorder.setState("待接收");
        List<Recycleorder> select4 = recycleorderService.select(recycleorder);
        recycleorder.setState("已完成");
        List<Recycleorder> select5 = recycleorderService.select(recycleorder);

        MemberLevel memberLevel = new MemberLevel();
        memberLevel.setVipId(user.getLevelId());
        List<MemberLevel> select6 = memberLevelService.select(memberLevel);



        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("我的");
        jsonResult.put("img",user.getImg());
        jsonResult.put("phone",user.getPhone());
        jsonResult.put("memberLevel",user.getLevelId());
        jsonResult.put("progress1",user.getAllPoints());
        jsonResult.put("progress1",select6.get(0).getLevel());
        jsonResult.put("signIn",user.getDays());
        jsonResult.put("points",user.getNowPoints());
        jsonResult.put("trading",select1.size());
        jsonResult.put("medals",select2.size());
        jsonResult.put("orderSize1",select3.size());
        jsonResult.put("orderSize2",select4.size());
        jsonResult.put("orderSize3",select5.size());
        return jsonResult.getDataMap();
    }

    @Autowired
    private RecycleorderService recycleorderService;

    @PostMapping("/findRecycleorderById")
    @ApiOperation("我的预约订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户Id", required = true),
            @ApiImplicitParam(name = "state", value = "订单状态", required = false)
    })
    public Map findRecycleorderById(Long id,String state){
        List<Map> recycleorderById = recycleorderService.findRecycleorderById(id, state);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(recycleorderById);
        jsonResult.setMsg("预约订单查询成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/cancelRecycleorderById")
    @ApiOperation("取消订单")
    @ApiImplicitParam(name = "id", value = "用户Id", required = true)
    public Map cancelRecycleorderById(Long id){
        Recycleorder recycleorder = recycleorderService.selectById(id);
        recycleorder.setState("已取消");
        recycleorderService.updateById(recycleorder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("取消订单");
        return jsonResult.getDataMap();
    }

    @Autowired
    private RecyclerService recyclerService;

    @PostMapping("/findRecycleitemById")
    @ApiOperation("订单详情")
    @ApiImplicitParam(name = "id",value = "订单Id", required = true)
    public Map findRecycleitemById(Long id){
        Recycleorder recycleorder = recycleorderService.selectById(id);
        List<Map> recycleorderDetails = recycleorderService.findRecycleorderDetails(id);
        Recycler recycler = recyclerService.selectById(recycleorder.getHid());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(recycleorderDetails);
        jsonResult.setMsg("订单详情查询成功");
        jsonResult.put("name",recycler.getName());
        jsonResult.put("phone",recycler.getPhone());
        jsonResult.put("time",recycleorder.getYtime());
        return jsonResult.getDataMap();
    }

    @Autowired
    private GoodsorderService goodsorderService;
    @Autowired
    ImagesRepository imagesRepository;
    @PostMapping("/findGoodsorder")
    @ApiOperation("我的交易")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户Id", required = true),
            @ApiImplicitParam(name = "state", value = "订单状态", required = false)
    })
    public Map findGoodsorder(Long id, String state){
        List<Map> goodsorder = goodsorderService.findGoodsorder(id, state);
        for(Map map:goodsorder){
            map.put("img",imagesRepository.findImgsByGidEquals((Long) map.get("gid")));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(goodsorder);
        jsonResult.setMsg("我的交易订单查询成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private UserService userService;
    @Autowired
    private ServiceAddressService serviceAddressService;

    @PostMapping("/findServiceAddress")
    @ApiOperation("管理地址")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map findServiceAddress(Long id){
        List<Map> address = serviceAddressService.findAddress(id);
        System.out.println("-------------\n"+address);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(address);
        jsonResult.setMsg("管理地址查询成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/insertServiceAddress")
    @ApiOperation("增加地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户Id", required = true),
            @ApiImplicitParam(name = "provinces",value = "省市区", required = true),
            @ApiImplicitParam(name = "name",value = "小区名", required = true),
            @ApiImplicitParam(name = "daddress",value = "详细地址", required = true),
            @ApiImplicitParam(name = "harvestName",value = "收货人姓名", required = true),
            @ApiImplicitParam(name = "harvestPhone",value = "收货人电话", required = true)
    })
    public Map insertServiceAddress(Long id,
                                    String provinces,
                                    String name,
                                    String daddress,
                                    String harvestName,
                                    String harvestPhone
                                    ){
        CommunityManage communityManage = new CommunityManage();
        ServiceAddress serviceAddress = new ServiceAddress();
        communityManage.setProvinces(provinces);
        List<CommunityManage> select = communityManageService.select(communityManage);
        if (select.size()==0){
            communityManage.setProvinces(provinces);
            communityManage.setName(name);
            communityManage.setCreateTime(new Date(System.currentTimeMillis()));
            communityManage.setRemarks("");
            communityManage.setState(true);
            communityManage.setAllocateTime(null);
            CommunityManage insert = communityManageService.insert(communityManage);
            serviceAddress.setCommnuityId(insert.getId());
        }
        else {
            serviceAddress.setCommnuityId(select.get(0).getId());
        }
        serviceAddress.setDaddress(daddress);
        serviceAddress.setUid(id);
        serviceAddress.setHarvestName(harvestName);
        serviceAddress.setHarvestPhone(harvestPhone);
        serviceAddressService.insert(serviceAddress);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("增加地址成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private CommunityManageService communityManageService;

    @PostMapping("/findUpdateServiceAddress")
    @ApiOperation("管理地址,查看修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceAddressId",value = "服务地址Id", required = true)
    })
    public Map findUpdateServiceAddress(Long serviceAddressId){
        ServiceAddress serviceAddress = serviceAddressService.selectById(serviceAddressId);
        CommunityManage communityManage = communityManageService.selectById(serviceAddress.getCommnuityId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("name",serviceAddress.getHarvestName());
        jsonResult.put("phone",serviceAddress.getHarvestPhone());
        jsonResult.put("provinces",communityManage.getProvinces());
        jsonResult.put("quarters",communityManage.getName());
        jsonResult.put("location",serviceAddress.getDaddress());
        jsonResult.setMsg("管理地址,查看修改地址");
        return jsonResult.getDataMap();
    }

    @PostMapping("/updateServiceAddress")
    @ApiOperation("管理地址,修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceAddressId",value = "服务地址Id", required = true),
            @ApiImplicitParam(name = "provinces",value = "省市区", required = true),
            @ApiImplicitParam(name = "quarters",value = "小区", required = true),
            @ApiImplicitParam(name = "location",value = "详细地址", required = true),
            @ApiImplicitParam(name = "harvestName",value = "收货人姓名", required = true),
            @ApiImplicitParam(name = "harvestPhone",value = "收货人电话", required = true)
    })
    public Map updateServiceAddress(Long serviceAddressId,
                                    String provinces,
                                    String quarters,
                                    String location,
                                    String harvestName,
                                    String harvestPhone
    ){
        ServiceAddress serviceAddress = serviceAddressService.selectById(serviceAddressId);
        serviceAddress.setDaddress(location);
        serviceAddress.setHarvestPhone(harvestPhone);
        serviceAddress.setHarvestName(harvestName);
        CommunityManage communityManage = new CommunityManage();
        communityManage.setProvinces(provinces);
        communityManage.setName(quarters);
        List<CommunityManage> select = communityManageService.select(communityManage);
        if (select.size()!=0){
            serviceAddress.setCommnuityId(select.get(0).getId());
        } else {
            communityManage.setCreateTime(new Date(System.currentTimeMillis()));
            communityManage.setRemarks("");
            communityManage.setState(true);
            communityManage.setAllocateTime(null);
            CommunityManage insert = communityManageService.insert(communityManage);
            serviceAddress.setCommnuityId(insert.getId());
        }
        serviceAddressService.updateById(serviceAddress);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("管理地址,修改地址");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteServiceAddress")
    @ApiOperation("删除地址")
    @ApiImplicitParam(name = "serviceAddressId",value = "地址Id", required = true)
    public Map deleteServiceAddress(Long serviceAddressId){
        serviceAddressService.deleteById(serviceAddressId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除地址");
        return jsonResult.getDataMap();
    }

    @Autowired
    private GetCycleService getCycleService;

    @PostMapping("/findGetCycle")
    @ApiOperation("周期上门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户Id", required = true) ,
            @ApiImplicitParam(name = "cycle", value = "周期",required = false)
    })
    public Map findGetCycle(Long id, Integer cycle){
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setUid(id);
        List<Recycleorder> select = recycleorderService.select(recycleorder);
        Recycleorder recycleorder1 = select.get(select.size()-1);
        Date ytime = recycleorder1.getYtime();
        String weekOfDate = DateUtil.getWeekOfDate(ytime);
        Date date = DateUtil.formatDate(ytime, cycle * 7);
        GetCycle getCycle1 = getCycleService.selectById(recycleorder1.getZid());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("");
        jsonResult.put("cycle",getCycle1.getCycle());
        jsonResult.put("week",weekOfDate);
        jsonResult.put("day",TimeUtil.timeFormat(DateUtil.formatDate(date, cycle * 7)));
        jsonResult.setMsg("周期上门");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getCycle")
    @ApiOperation("得到周期上门时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户Id", required = true) ,
            @ApiImplicitParam(name = "cycle", value = "周期",required = true)
    })
    public Map getcycle(Long id,Integer cycle) throws BusinessException {
        User user = userService.selectById(id);

        GetCycle getCycle = new GetCycle();
        getCycle.setCycle(cycle);
        List<GetCycle> select1 = getCycleService.select(getCycle);

        ServiceAddress serviceAddress = new ServiceAddress();
        serviceAddress.setDaddress(user.getLocation());
       /* List<ServiceAddress> select = serviceAddressService.select(serviceAddress);
        if (select.size()!=0 && select1.size()!=0){
            Recycleorder recycleorder = new Recycleorder();
            recycleorder.setState("预约中");
            recycleorder.setUid(id);
            recycleorder.setYtime(DateUtil.formatDate(new Date(System.currentTimeMillis()), cycle * 7));
            recycleorder.setDid(select.get(0).getId());
            recycleorder.setOrdertime(new Date(System.currentTimeMillis()));
            recycleorder.setZid(select1.get(0).getId());
            recycleorder.setCode("1234");
            recycleorderService.insert(recycleorder);


        }
        throw new BusinessException(ExceptionCode.NO_USER_PLACE);*/

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("");
        jsonResult.setMsg("周期上门时间");
        jsonResult.put("cycle",cycle);
        jsonResult.put("nowTime", TimeUtil.timeFormat(DateUtil.formatDate(new Date(System.currentTimeMillis()), cycle * 7)));
        return jsonResult.getDataMap();
    }

    @Autowired
    private AdviceService adviceService;

    @PostMapping("accountSet")
    @ApiOperation("账户设置")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map accountSet(Long id){
        Advice advice = new Advice();
        advice.setUid(id);
        List<Advice> select = adviceService.select(advice);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(select);
        jsonResult.setMsg("账户设置");
        return jsonResult.getDataMap();
    }

    @PostMapping("/acceptAdvice")
    @ApiOperation("接受建议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户Id", required = true),
            @ApiImplicitParam(name = "message",value = "建议信息", required = true)
    })
    public Map acceptAdvice(Long id, String message){
        Advice advice = new Advice();
        advice.setUid(id);
        advice.setCreattime(new Date(System.currentTimeMillis()));
        advice.setWords(message);
        adviceService.insert(advice);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("接受建议");
        return jsonResult.getDataMap();
    }

    @Autowired
    private ScoreService scoreService;

    @PostMapping("/findScore")
    @ApiOperation("积分明细")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map findScore(Long id){
        Score score = new Score();
        score.setUid(id);
        List<Score> select = scoreService.select(score);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(select);
        jsonResult.setMsg("积分明细");
        return jsonResult.getDataMap();
    }
    @Autowired
    private MedalService medalService;
    @Autowired
    private MemberLevelService memberLevelService;

    @PostMapping("/findMedal")
    @ApiOperation("我的勋章")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map findMedal(Long id){
        User user = userService.selectById(id);
        List<Map> medal = medalService.findMedal(id);
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setUid(id);
        List<Recycleorder> select = recycleorderService.select(recycleorder);

        MemberLevel memberLevel = new MemberLevel();
        memberLevel.setVipId(user.getLevelId());
        List<MemberLevel> select1 = memberLevelService.select(memberLevel);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(medal);
        jsonResult.put("nickName",user.getNickName());
        jsonResult.put("img",user.getImg());
        jsonResult.put("times",select.size());
        jsonResult.put("memberLevel",user.getLevelId());
        jsonResult.put("progress1",user.getAllPoints());
        jsonResult.put("progress2",select1.get(0).getLevel());
        jsonResult.setMsg("我的勋章");
        return jsonResult.getDataMap();
    }

    @PostMapping("/findMemberLevel")
    @ApiOperation("会员级别")
    @ApiImplicitParam(name = "id",value = "用户Id", required = true)
    public Map findMemberLevel(Long id){
        List<MemberLevel> memberLevels = memberLevelService.selectAll();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(memberLevels);
        jsonResult.setMsg("会员级别");
        return jsonResult.getDataMap();
    }

}
